// minepackage/pages/editinfo/editinfo.js
import { areaList } from '@vant/area-data'
import Dialog from '@vant/weapp/dialog/dialog'
import Toast from '@vant/weapp/toast/toast'
import { addMineInfo, sendPhoneCode, sendEmailCode } from '../../api/editInfoApi.js'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userJSON: null,
    //上个页面条目标识
    pageItemCode: '',
    userName: '',
    nickName: '',
    sex: '1',
    birthday: '',
    address: '',
    addressDetail: '',
    phone: '',
    phoneCode: '',
    email: '',
    emailCode: '',
    areaList,
    nowDay: new Date().getTime(), //当前时间
    minDate: 315504000, //最小时间
    formatter(type, value) {  //格式化
      if (type === 'year') {
        return `${value}年`;
      }
      if (type === 'month') {
        return `${value}月`;
      }
      if (type === 'day') {
        return `${value}日`;
      }
      return value;
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(o) {
    const userDataStr = wx.getStorageSync('user_data')
    if (userDataStr == null || userDataStr == '') {
      wx.reLaunch({
        url: '/loginpackage/pages/login/login',
      })
    } else {
      this.setData({
        userJSON: JSON.parse(userDataStr)
      })
      const code = o.code
      this.setData({
        pageItemCode: code
      })
    }
  },

  /**
   * 单选按钮点击事件
   */
  sexOnChange(event) {
    this.setData({
      sex: event.detail,
    });
  },

  /**
   * 单选条目事件
   */
  itemOnClick(event) {
    const { name } = event.currentTarget.dataset;
    this.setData({
      sex: name,
    });
  },

  /**
   * 日期改变事件
   */
  dateOnInput(event) {
    const timestamp = event.detail // 待转换的时间戳
    const date = new Date(timestamp) // 根据时间戳创建Date对象
    const year = date.getFullYear() // 获取年份
    const month = date.getMonth() + 1 // 获取月份，需要加1
    const day = date.getDate() // 获取日期
    this.setData({
      birthday: year + '年' + month + '月' + day + '日',
    });
  },

  /**
   * 家庭住址选项发生改变时触发
   */
  addressChange(e) {
    let addressVal = ''
    const addressArr = e.detail.values
    addressArr.forEach(item => {
      addressVal += item.name
    });
    this.setData({
      address: addressVal
    })
  },

  /**
   * 获取姓名输入框值
   */
  nameChange(e) {
    this.setData({
      userName: e.detail
    })
  },

  /**
   * 获取昵称输入框值
   */
  nickNameChange(e) {
    this.setData({
      nickName: e.detail
    })
  },

  /**
   * 详细地址输入框值
   */
  aDetailChange(e) {
    this.setData({
      addressDetail: e.detail
    })
  },

  /**
   * 获取电话号码
   */
  phoneChange(e) {
    this.setData({
      phone: e.detail
    })
  },

  /**
   * 获取电话号码验证码值
   */
  phoneCodeChange(e) {
    this.setData({
      phoneCode: e.detail
    })
  },

  /**
   * 获取邮箱
   */
  emailChange(e) {
    this.setData({
      email: e.detail
    })
  },

  /**
   * 获取邮箱验证码值
   */
  emailCodeChange(e) {
    this.setData({
      emailCode: e.detail
    })
  },

  /**
   * 发送电话验证码
   */
  sendPhoneClick() {
    const params = {
      phone: this.data.phone
    }
    sendPhoneCode(params, res => {
      Dialog.alert({
        title: '电话验证码',
        message: '您的验证码是：' + res.msg,
      }).then(() => {
        this.setData({
          phoneCode: res.msg
        })
      });
    }, e => {
      console.log(e)
    })
  },

  /**
   * 发送邮箱验证码
   */
  sendEmailClick() {
    const params = {
      email: this.data.email
    }
    sendEmailCode(params, res => {
      Dialog.alert({
        title: '邮箱验证码',
        message: '您的验证码是：' + res.msg,
      }).then(() => {
        this.setData({
          emailCode: res.msg
        })
      });
    }, e => {
      console.log(e)
    })
  },

  /**
   * 保存按钮
   */
  saveClick() {
    this.editUserInfoRequest()
  },

  /**
   * 修改用户信息请求
   */
  editUserInfoRequest() {
    const id = this.data.userJSON.id
    Toast.loading({
      message: '正在保存...',
      forbidClick: true
    })
    switch (this.data.pageItemCode) {
      case '1': //姓名
        const params1 = {
          id: id,
          userName: this.data.userName
        }
        addMineInfo(params1, res => {
          Toast(res.msg)
        }, e => {
          console.log(e)
        })
        break
      case '2': //昵称
        const params2 = {
          id: id,
          nickName: this.data.nickName
        }
        addMineInfo(params2, res => {
          Toast(res.msg)
        }, e => {
          console.log(e)
        })
        break
      case '3': //性别
        const params3 = {
          id: id,
          sex: this.data.sex
        }
        addMineInfo(params3, res => {
          Toast(res.msg)
        }, e => {
          console.log(e)
        })
        break
      case '4': //出生日期
        let date = this.data.birthday
        date = date.replaceAll('年', '-')
        date = date.replaceAll('月', '-')
        date = date.replaceAll('日', '')
        const params4 = {
          id: id,
          birthday: new Date(date)
        }
        addMineInfo(params4, res => {
          Toast(res.msg)
        }, e => {
          console.log(e)
        })
        break
      case '5': //家庭住址
        const params5 = {
          id: id,
          address: this.data.address
        }
        addMineInfo(params5, res => {
          Toast(res.msg)
        }, e => {
          console.log(e)
        })
        break
      case '6': //家庭详细地址
        const params6 = {
          id: id,
          addressDetail: this.data.addressDetail
        }
        addMineInfo(params6, res => {
          Toast(res.msg)
        }, e => {
          console.log(e)
        })
        break
      case '7': //电话号码
        const params7 = {
          id: id,
          phone: this.data.phone,
          phoneCode: this.data.phoneCode
        }
        addMineInfo(params7, res => {
          Toast(res.msg)
        }, e => {
          console.log(e)
        })
        break
      case '8': //电子邮箱
        const params8 = {
          id: id,
          email: this.data.email,
          emailCode: this.data.emailCode
        }
        addMineInfo(params8, res => {
          Toast(res.msg)
        }, e => {
          console.log(e)
        })
        break
    }
  }
})